﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Keepo.Models;
using Keepo.Models.Services;
using Keepo.Helpers;
using AlbumDesigner.Models.Enums;
using AlbumDesigner.Helpers;

namespace AlbumDesigner.Controllers
{
    public class MasksController : Controller
    {
        private AlbumContext db = new AlbumContext();

        [CheckPermission(section = "Masks")]
        public ActionResult Index()
        {
            //get the corners
            var cornersList = new List<SelectListItem>();
            cornersList.Add(new SelectListItem() { Value = "miter", Text = "זוויתי" });
            cornersList.Add(new SelectListItem() { Value = "round", Text = "מעוגל" });
            ViewBag.CornersList = cornersList;

            return View();
        }

        [CheckPermission(section = "Masks")]
        [HttpPost]
        public ActionResult GetMasksList(int Page = 1)
        {
            //get all the masks (without AlbumId)
            var masksList = db.DesignMasks.Where(m => m.AlbumId == null).ToList();

            var masks = masksList.Select(m => new DesignMaskExport(m)).ToList();

            //add the Paging settings to the masks list
            int rowsPerPage = 10;
            int masksCount = masks.Count();
            int startIndex = (Page - 1) * rowsPerPage;
            int pagesCount = masksCount / rowsPerPage;
            if (masksCount % rowsPerPage > 0)
            {
                pagesCount++;
            }

            var masksPages = masks.Skip(startIndex).Take(rowsPerPage);

            return Json(new { Masks = masksPages, Pages = pagesCount });
        }

        [CheckPermission(section = "Masks")]
        [HttpPost]
        public ActionResult Update(DesignMaskExport mask)
        {
            //find the specified mask and update its details
            var existMask = db.DesignMasks.Find(mask.DesignMaskId);

            //the mask exists in DB
            if (existMask != null)
            {
                //update the relevant fields
                existMask.Title = mask.Title;
                existMask.MaskData = mask.MaskData;
                existMask.Color = mask.Color;
                existMask.Weight = mask.Weight;
                existMask.Corner = mask.Corner;

                db.SaveChanges();

                return Json(existMask.DesignMaskId);
            }
            //the mask is new
            else
            {
                var newMask = new DesignMask();
                newMask.Title = mask.Title;
                newMask.MaskData = mask.MaskData;
                newMask.Color = mask.Color;
                newMask.Weight = mask.Weight;
                newMask.Corner = mask.Corner;

                db.DesignMasks.Add(newMask);
                db.SaveChanges();

                return Json(newMask.DesignMaskId);
            }
        }

        [CheckPermission(section = "Masks")]
        [HttpPost]
        public ActionResult Delete(int id)
        {
            //find the Mask and delete it
            var Mask = db.DesignMasks.Find(id);
            db.DesignMasks.Remove(Mask);
            db.SaveChanges();

            return Json(new KeyValuePair<bool, string>(true, "המסכה נמחקה בהצלחה."));
        }

        [HttpPost]
        public ActionResult AddDesignMask(DesignMaskExport designMask)
        {
            return Json(AlbumsLogic.addDesignMask(designMask));
        }

        // Flex
        // In case albumId is -1, returns general masks
        [HttpPost]
        public ActionResult GetDesignMasksByAlbumId(int albumId)
        {
            return Json(AlbumsLogic.getDesignMasks(albumId));
        }
    }
}
